package com.example.demo.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Controller
public class HelloController {

    @ResponseBody
    @GetMapping("/")
    public ModelAndView hello() {
        return new ModelAndView("index");
//        return new ModelAndView("welcome");
    }

    @PostMapping("/upload")
    public String singleFileUpload(@RequestParam("file") MultipartFile file,
                                   RedirectAttributes redirectAttributes) {
        if (file.isEmpty()) {
            redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
            return "redirect:uploadStatus";
        }
        try {
            // Get the file and save it somewhere
            byte[] bytes = file.getBytes();
            // UPLOADED_FOLDER ⽂件本地存储地址

            Path path = Paths.get("E:\\" + file.getOriginalFilename());
            Files.write(path, bytes);
            redirectAttributes.addFlashAttribute("message",
                    "You successfully uploaded '" + file.getOriginalFilename() + "'");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/uploadStatus";
    }

    @ResponseBody
    @GetMapping("/uploadStatus")
    public ModelAndView uploadStatus() {
        return new ModelAndView("uploadStatus");
    }

    @ResponseBody
    @GetMapping("/imageSynthesis")
    public String imageSynthesis() {
        String backgroundPath = "D:\\back.jpg";
        String qrCodePath = "D:\\avatar.png";
        String message01 ="扫描下方二维码，欢迎大家添加我的淘宝返利机器人";
        String message02 = "居家必备，省钱购物专属小秘书！";
        String outPutPath="D:\\end.jpg";
        overlapImage(backgroundPath,qrCodePath,message01,message02,outPutPath);
        return null;
    }

    public static void main(String[] args) {
        String backgroundPath = "D:\\back.jpg";
        String qrCodePath = "D:\\avatar.png";
        String message01 ="钟正";
        String message02 = "支撑部研发";
        String outPutPath="D:\\end.jpg";
        overlapImage(backgroundPath,qrCodePath,message01,message02,outPutPath);
    }

    public static String overlapImage(String backgroundPath,String qrCodePath,String message01,String message02,String outPutPath){
        try {
            //设置图片大小
//            BufferedImage background = resizeImage(618,1000, ImageIO.read(new File("这里是背景图片的路径！")));
            BufferedImage background = resizeImage(500,765, ImageIO.read(new File(backgroundPath)));
//            BufferedImage qrCode = resizeImage(150,150,ImageIO.read(new File("这里是插入二维码图片的路径！")));
            BufferedImage qrCode = resizeImage(140,185,ImageIO.read(new File(qrCodePath)));
            //在背景图片中添加入需要写入的信息，例如：扫描下方二维码，欢迎大家添加我的淘宝返利机器人，居家必备，省钱购物专属小秘书！
            //String message = "扫描下方二维码，欢迎大家添加我的淘宝返利机器人，居家必备，省钱购物专属小秘书！";
            Graphics2D g = background.createGraphics();
            g.setColor(Color.BLACK);
            g.setFont(new Font("黑体",Font.BOLD,32));
            g.drawString(message01,230 ,460);
            g.drawString(message02,230 ,520);
            //在背景图片上添加二维码图片
            g.drawImage(qrCode, 190, 210, qrCode.getWidth(), qrCode.getHeight(), null);
            g.dispose();
//            ImageIO.write(background, "jpg", new File("这里是一个输出图片的路径"));
            ImageIO.write(background, "jpg", new File(outPutPath));
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static BufferedImage resizeImage(int x, int y, BufferedImage bfi){
        BufferedImage bufferedImage = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
        bufferedImage.getGraphics().drawImage(
                bfi.getScaledInstance(x, y, Image.SCALE_SMOOTH), 0, 0, null);
        return bufferedImage;
    }

    public static String overlapImage(){
        try {
            //设置图片大小
            BufferedImage background = resizeImage(618,1000, ImageIO.read(new File("这里是背景图片的路径！")));
            BufferedImage qrCode = resizeImage(150,150,ImageIO.read(new File("这里是插入二维码图片的路径！")));

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
